package lanou3g.myfirstapp36kr.fragment;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import lanou3g.myfirstapp36kr.BuildConfig;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.activity.MainActivity;
import lanou3g.myfirstapp36kr.base.BaseFragment;
import lanou3g.myfirstapp36kr.entity.MyColectionEntity;
import lanou3g.myfirstapp36kr.entity.QQBean;
import lanou3g.myfirstapp36kr.myvolleysingleton.Colection;
import lanou3g.myfirstapp36kr.myvolleysingleton.DaoMasterSingleton;

/**
 * Created by dllo on 16/8/30.
 */
public class LoginFragment extends BaseFragment {
    private Platform qq;
    private EditText editTextAccount;
    private EditText editTextPassword;
    private Button buttonLogin;
    private ImageView imageViewqq;
    @Override
    protected int getLayout() {
        return R.layout.fragment_login;
    }

    @Override
    protected void initView(View view) {
        editTextAccount = (EditText) view.findViewById(R.id.et_account);
        editTextPassword = (EditText) view.findViewById(R.id.et_password);
        buttonLogin = (Button) view.findViewById(R.id.btn_login);
        imageViewqq = (ImageView) view.findViewById(R.id.iv_qq_login);

    }

    @Override
    protected void initData() {
        buttonLogin.setOnClickListener(new OnClickListener() {

            private BmobUser bmobUser;

            @Override
            public void onClick(View view) {
                String accountStr = editTextAccount.getText().toString();
                String passwordStr = editTextPassword.getText().toString();
                bmobUser = new BmobUser();
                bmobUser.setUsername(accountStr);
                bmobUser.setPassword(passwordStr);
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null){
                            Intent intent = new Intent(LoginFragment.this.getActivity(),
                                    MainActivity.class);
                            startActivity(intent);
                            editTextAccount.setText("");
                            editTextPassword.setText("");

                            //从bmob网上拉取数据,更新本地数据库
                            //把本地数据库全部删除
                            DaoMasterSingleton.getDaoMasterSingleton().getColectionDao().deleteAll();
                            BmobQuery<MyColectionEntity> bmobQuery = new BmobQuery<MyColectionEntity>();
                            bmobQuery.addWhereEqualTo("userName",bmobUser.getUsername());
                            bmobQuery.findObjects(new FindListener<MyColectionEntity>() {
                                @Override
                                public void done(List<MyColectionEntity> list, BmobException e) {
                                    for (MyColectionEntity myColectionEntity : list) {
                                        Colection colection = new Colection();
                                        colection.setTitle(myColectionEntity.getTitle());
                                        colection.setColumnName(myColectionEntity.getColumnName());
                                        colection.setName(myColectionEntity.getName());
                                        colection.setFeatureImg(myColectionEntity.getFeatureImg());
                                        colection.setDetailsUrl(myColectionEntity.getDetailUrl());
                                        colection.setTime(myColectionEntity.getTime());
                                        DaoMasterSingleton.getDaoMasterSingleton().getColectionDao().insert(colection);
                                    }
                                }
                            });

                        }else {
                            if (BuildConfig.DEBUG) Log.d("LoginFragment", "e:" + e);
                            Toast.makeText(LoginFragment.this.getActivity(), "不对别想蒙进来", Toast.LENGTH_SHORT).show();
                            editTextAccount.setText("");
                            editTextPassword.setText("");
                        }

                    }
                });
            }
        });
        imageViewqq.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        FragmentManager manager = getActivity().getSupportFragmentManager();


                        Intent intent = new Intent("load is success");
                        context.sendBroadcast(intent);

                        QQBean bean = new QQBean();
                        bean.setQQIcon(qq.getDb().getUserIcon());
                        bean.setQQId(qq.getDb().getUserId());
                        bean.setQQName(qq.getDb().getUserName());
                        bean.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {
                                    Toast.makeText(context, "添加数据成功:" + s, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(context, "失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                    }
                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                qq.authorize();
            }
        });

    }
}
